﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OpticalPowerMeter.UserControls
{
    /// <summary>
    /// SnEnterControl.xaml 的交互逻辑
    /// </summary>
    public partial class SnEnterControl : UserControl
    {
        public SnEnterControl()
        {
            InitializeComponent();
        }


        //字号
        private static DependencyProperty MyFontSizeProperty = DependencyProperty.Register(
            "MyFontSize",
            typeof(double),
            typeof(SnEnterControl)
            );

        //文本框高度
        private static DependencyProperty TextHeightProperty = DependencyProperty.Register(
            "TextHeight",
            typeof(double),
            typeof(SnEnterControl)
            );

        //文本框宽度
        private static DependencyProperty TextWidthProperty = DependencyProperty.Register(
            "TextWidth",
            typeof(double),
            typeof(SnEnterControl)
            );

        //文本框外边距
        private static DependencyProperty TextMarginProperty = DependencyProperty.Register(
            "TextMargin",
            typeof(Thickness),
            typeof(SnEnterControl)
            );

        //Sn值
        private static DependencyProperty SNProperty = DependencyProperty.Register(
            "SN",
            typeof(string),
            typeof(SnEnterControl)
            );

        public string Sn
        {
            get { return (string)GetValue(SNProperty); }
        }

        //尺寸发生变化时，调整控件大小
        private void ControlSizeChanged(object sender,SizeChangedEventArgs args) {
            double height = Math.Round(this.ActualHeight, 2);
            double width = Math.Round(this.ActualWidth, 2);

            double labelWidth = width * 0.3; //标签占30%宽度
            double fontSizeOfWidth = Math.Round((labelWidth - 10)/1.39,1); //根据宽度计算的字号
            double fontSizeOfHeight = Math.Round((height - 10) / 1.17, 1); //根据高度计算的字号
            double fontSize = fontSizeOfWidth < fontSizeOfHeight ? fontSizeOfWidth : fontSizeOfHeight; //取小的字号
            SetValue(MyFontSizeProperty,fontSize);  //设置字号
            double labelWidthActure = fontSize * 1.39 + 10;
            double textLeft = Math.Round(labelWidthActure + 5, 1);
            SetValue(TextMarginProperty, new Thickness(textLeft, 0, 0, 0)); //文本框边距
            SetValue(TextWidthProperty,Math.Round(width - labelWidthActure - 10)); //文本框宽度
            SetValue(TextHeightProperty,height-10); //文本框高度
        }
    }
}
